<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="弹出层" />
<meta name="description" content="脚本之家演示平台，演示XHTML、CSS、jquery、PHP案例和示例" />
<title>演示：HTML5+CSS3+jQuery实现弹出层</title>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="http://www.helloweba.com/demo/css/demo.css" />
<link rel="stylesheet" href="hwLayer.css">
</head>
<body>
<div class="container">
	<header>
		<div class="row">
			
		</div>
	</header>
	<div class="row main">
		<div class="col-md-12">
			<h2 class="top_title"><span class="glyphicon glyphicon-menu-left" aria-hidden="true"></span><a href="http://www.jb51.net/view-blog-365.html">HTML5+CSS3+jQuery实现弹出层</a></h2>
			<div class="optlist">
				<a class="btn btn-info btn-lg show-layer" data-show-layer="hw-layer" role="button">点击弹出层</a>
			</div>
			
			
			<div class="hw-overlay" id="hw-layer">
				<div class="hw-layer-wrap">
					<span class="glyphicon glyphicon-remove hwLayer-close"></span>
					<div class="row">
						<div class="col-md-3 col-sm-12 hw-icon">
							<i class="glyphicon glyphicon-info-sign"></i>
						</div>
						<div class="col-md-9 col-sm-12">
							<h4>你确定吗？</h4>
							<p>这是HTML+CSS+Javascript实现的一个弹出层效果，它是响应式的，它可以在所有现代浏览器上工作（包括PC和移动端）。</p>

							<button class="btn btn-success hwLayer-ok">确 定</button>
							<button class="btn btn-warning hwLayer-cancel">取 消</button>
						</div>
					</div>
				</div>
			</div>

		</div>
	</div>
	<footer>
	
	</footer>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function(){
	//展示层
	function showLayer(id){
		var layer = $('#'+id),
			layerwrap = layer.find('.hw-layer-wrap');
		layer.fadeIn();
		//屏幕居中
		layerwrap.css({
			'margin-top': -layerwrap.outerHeight()/2
		});
	}

	//隐藏层
	function hideLayer(){
		$('.hw-overlay').fadeOut();
	}

	$('.hwLayer-ok,.hwLayer-cancel,.hwLayer-close').on('click', function() {
		hideLayer();
	});

	//触发弹出层
	$('.show-layer').on('click',  function() {		
		var layerid = $(this).data('show-layer');
		showLayer(layerid);
	});

	//点击或者触控弹出层外的半透明遮罩层，关闭弹出层
	$('.hw-overlay').on('click',  function(event) {
		if (event.target == this){
			hideLayer();
		}
	});

	//按ESC键关闭弹出层
	$(document).keyup(function(event) {
		if (event.keyCode == 27) {
			hideLayer();
		}
	});
});
</script>
</body>
</html>